﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WindowsLive;


namespace goMatic
{
    public partial class ConfigHM : System.Web.UI.Page
    {
        const string LoginCookie = "webauthtoken";

        // Initialize the WindowsLiveLogin module.
        static WindowsLiveLogin wll = new WindowsLiveLogin(true);

        protected static string AppId = wll.AppId;
        protected string UserId;
        protected string CustomerId;
        protected HMConfig oHMConfig;
        protected dbEntities oDB;
        protected void Page_Load(object sender, EventArgs e)
        {
            /* If the user token has been cached in a site cookie, attempt
               to process it and extract the user ID. */
            if (!Page.IsPostBack)
            {
                tbExternalIP.Text = Request.ServerVariables["REMOTE_ADDR"];
                cbSSL.Checked = false;

                HttpRequest req = HttpContext.Current.Request;
                HttpCookie loginCookie = req.Cookies[LoginCookie];

                if (loginCookie != null)
                {
                    string token = loginCookie.Value;

                    if (!string.IsNullOrEmpty(token))
                    {
                        WindowsLiveLogin.User user = wll.ProcessToken(token);

                        if (user != null)
                        {
                            UserId = user.Id;
                            dbEntities oDB = new dbEntities();
                            CustomerId = oDB.Customers.First(t => t.liveid == UserId).customerid;
                            ViewState.Add("CustomerID", CustomerId);

                            if (oDB.HMConfig.Count(t => t.customerid == CustomerId) > 0)
                            {
                                oHMConfig = oDB.HMConfig.First(t => t.customerid == CustomerId);
                                if (!string.IsNullOrEmpty(oHMConfig.external_ip))
                                    tbExternalIP.Text = oHMConfig.external_ip;

                                tbAccount.Text = oHMConfig.username;
                                //required if password mode is enabled.
                                tbPassword.Attributes.Add("value", oHMConfig.password);
                                tbPort.Text = oHMConfig.tcpport.ToString();
                                try
                                {
                                    cbSSL.Checked = (bool)oHMConfig.ssl;
                                }
                                catch { cbSSL.Checked = false; }
                            }
                        }
                    }
                }
                else
                {
                    Response.Redirect("~/Default.aspx");
                }
            }
            else
            {
                CustomerId = ViewState["CustomerID"].ToString();
                if (string.IsNullOrEmpty(CustomerId))
                {
                    Response.Redirect("~/Default.aspx");
                }
            }
        }

        protected void btSave_Click(object sender, EventArgs e)
        {
            dbEntities oDB = new dbEntities();
            bool bNew = false;
            if (oDB.HMConfig.Count(t => t.customerid == CustomerId) == 0)
            {
                oHMConfig = new HMConfig();
                bNew = true;
            }
            else
            {
                oHMConfig = oDB.HMConfig.First(t => t.customerid == CustomerId);

            }
            oHMConfig.external_ip = tbExternalIP.Text;
            oHMConfig.tcpport = int.Parse(tbPort.Text);
            oHMConfig.username = tbAccount.Text;
            oHMConfig.password = tbPassword.Text;
            oHMConfig.ssl = cbSSL.Checked;

            if (bNew)
            {
                oHMConfig.customerid = CustomerId;
                oHMConfig.sessiontimeout = 300;
                oHMConfig.ccusessiontoken = "none";
                oHMConfig.C__createdAt = DateTime.Now;
                oDB.HMConfig.Add(oHMConfig);
            }
            oDB.SaveChanges();

            Response.Redirect("~/Start.aspx");
        }
    }
}